package com.atguigu.servlet;

import com.atguigu.bean.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ Author:任留辉
 * @ Description:
 * @ Date:Created in 23:28 2021/3/14 0014
 */
public class UserServlet extends BaseServlet {

    /**
     * 处理登录的请求和响应
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //调用service中的方法处理业务逻辑，实现登录验证
        UserService userService = new UserServiceImpl();
        User user = userService.checkLogin(username, password);
        //判断验证登录的结果
        if (user != null) {
            //获取session对象，pageContext.getSession()，request.getSession()
            HttpSession session = request.getSession();
            //将用户信息在session中进行共享
            session.setAttribute("user", user);
            //表示登录成功，需要重定向到登录成功页面
            response.sendRedirect(request.getContextPath() + "/pages/user/login_success.jsp");
        } else {
            //在请求域request中共享错误信息
            request.setAttribute("errorMsg", "用户名或密码错误");
            //表示登录失败，需要转发到登录页面
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
        }
    }

    /**
     * 处理注册的请求和响应
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        //获取session
        HttpSession session = request.getSession();
        //通过session获取正确的验证码
        String sessionCode = (String) session.getAttribute("session_code");
        //对注册的业务逻辑处理的结果进行判断
        //验证验证码是否正确
        if(code.equals(sessionCode)){
            //验证码正确，执行注册功能
            //调用service处理业务逻辑，处理注册功能
            UserService userService = new UserServiceImpl();
            //将用户信息封装到user对象中
            User user = new User(null, username, password, email);
            boolean flag = userService.regist(user);
            //对注册的业务逻辑处理的结果进行判断
            if (flag) {
                //注册成功，需要重定向到注册成功页面
                response.sendRedirect(request.getContextPath() + "/pages/user/regist_success.jsp");
            } else {
                //在请求域request中共享错误信息
                request.setAttribute("errorMsg", "用户名已经被注册");
                //注册失败，需要转发到注册页面
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            }
        }else{
            //验证码错误
            //在请求域request中共享错误信息
            request.setAttribute("errorMsg", "验证码错误");
            //注册失败，需要转发到注册页面
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }



    /**
     * 注销
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session
        HttpSession session = request.getSession();
        //强制使session失效
        session.invalidate();
        //重定向到首页
        response.sendRedirect(request.getContextPath() + "/");
    }
}